import util from '../../../utils/util.js';
import $ from '../../../utils/ajax.js';
const app = getApp();
Page({
    data: {
        onDate: {
            companyName: '',
            companyAddr: '',
            legalPerson: '',
            legalPersonTel: '',
            dutyPerson: '',
            dutyPersonTel: '',
            accountBank: '',
            accountName: '',
            accountNum: '',
            province: null,
            city: null
        },
        multiIndex: [0, 0],
        multiArray: [],
        multiIndex1: [0, 0],
        multiArray1: [],
        item: {},
        second: 0,
    },
    onLoad(options) {
        let second = options.second ? Number(options.second) : 0;
        if (second === 100) {
            const item = JSON.parse(options.item)
            this.region({
                province: null,
                role: item.role,
                city: null,
            });
            this.setData({
                item: item,
                second: second
            })
        } else {
            let prime = JSON.parse(options.item)
            const dateUrl = {
                companyName: prime.companyName || '',
                companyAddr: prime.companyAddr || '',
                legalPerson: prime.legalPerson || '',
                legalPersonTel: prime.legalPersonTel || '',
                dutyPerson: prime.dutyPerson || '',
                dutyPersonTel: prime.dutyPersonTel || '',
                accountBank: prime.accountBank || '',
                accountName: prime.accountName || '',
                accountNum: prime.accountNum || '',
            }
            this.setData({
                onDate: dateUrl,
                item: prime
            })
            this.region(prime);
        }

    },
    region(options){
        $.p(app.wxApp.url + 'agentnew/region', {
            parentId: 1
        }, () => {}, (res) => {
            let province = 0;
            res.data.map((item, index) => {
                if (item.id === options.province) {
                    province = index;
                }
            })
            this.setData({
                multiArray: [res.data],
                multiIndex: [province],
                multiArray1: [res.data],
                multiIndex1: [province],
                agentType: Number(options.role)
            },() => {
                if (this.data.agentType === 2) {
                    $.p(app.wxApp.url + 'agentnew/region', {
                        parentId: this.data.multiArray[0][this.data.multiIndex[0]].id || 0
                    }, () => {}, (res) => {
                        //this.data.multiIndex[0]
                        let data = {
                            multiArray: this.data.multiArray,
                            multiIndex: this.data.multiIndex,
                            multiArray1: this.data.multiArray1,
                            multiIndex1: this.data.multiIndex1,
                        };
                        data.multiArray1[1] = data.multiArray[1] = res.data;
                        data.multiIndex1[1] = data.multiIndex[1] = 0;
                        res.data.map((item,index)=>{
                            if (item.id === options.city) {
                                data.multiIndex1[1] = data.multiIndex[1] = index;
                            }
                        })
                        this.setData(data);
                    })
                }
            })
            app.onShop();
        });
    },

    onInput(e) {
        this.data.onDate[e.target.id] = e.detail.value;
    },
    bindMultiPickerColumnChange(e) {
        let data = {
            multiArray1: this.data.multiArray1,
            multiIndex1: this.data.multiIndex1,
        };
        data.multiIndex1[e.detail.column] = e.detail.value;
        if (e.detail.column === 0 && this.data.agentType === 2) {
            $.p(app.wxApp.url + 'agentnew/region', {
                parentId: data.multiArray1[e.detail.column][e.detail.value].id
            }, () => {}, (res) => {
                app.onShop();
                data.multiArray1[1] = res.data;
                data.multiIndex1[1] = 0;
                console.log(data)
                this.setData(data);
            })
        }
        this.setData(data);
    },
    bindcancel(e) {
        this.setData({
            multiArray1: this.data.multiArray,
            multiIndex1: this.data.multiIndex,
        });
    },
    bindchange(e) {
        this.setData({
            multiArray: this.data.multiArray1,
            multiIndex: this.data.multiIndex1,
        });
    },


    onPrev() {
        $.p(app.wxApp.url + 'agentnew/backStep', {
            tel: wx.getStorageSync('tel'),
            agent: wx.getStorageSync('agent'),
            type: 1,
        }, () => { }, (res) => {
            app.agentStatus();
        })
    },
    onSubmit() {
        const prime = this.data.onDate;
        const multiArray = this.data.multiArray;
        const multiIndex = this.data.multiIndex;
        const agentType = this.data.agentType;
        const role = this.data.item.role;
        const second = this.data.second
        const generalCheck = 
            util.nonEmpty(prime.companyName, '请填写公司名称') &&
            util.nonEmpty(prime.companyAddr, '请填写公司地址') &&
            util.nonEmpty(prime.legalPerson, '请填写公司法人') &&
            util.telCheck(prime.legalPersonTel, '法人联系电话') &&
            util.nonEmpty(prime.dutyPerson, '请填写公司负责人') &&
            util.telCheck(prime.dutyPersonTel, '负责人联系电话') &&
            util.nonEmpty(prime.accountBank, '请填写开户行') &&
            util.nonEmpty(prime.accountName, '请填写账户名称') &&
            util.CheckBankNo(prime.accountNum);
        if (generalCheck) {
            if (second === 100) {
                let item = {
                    ...this.data.item,
                    ...prime,
                }
                if (agentType === 1) {
                    item.province = multiArray[0][multiIndex[0]].id;
                } else if (agentType === 2) {
                    item.province = multiArray[0][multiIndex[0]].id;
                    item.city = multiArray[1][multiIndex[1]].id;
                }
                wx.navigateTo({
                    url: '/pages/examine/photo/photo?second=100&item=' + JSON.stringify(item)
                })
            } else {
                let role = {
                    ...prime,
                    agent: wx.getStorageSync('agent'),
                    role: this.data.item.role
                }
                if (agentType === 1) {
                    role.province = multiArray[0][multiIndex[0]].id;
                } else if (agentType === 2) {
                    role.province = multiArray[0][multiIndex[0]].id;
                    role.city = multiArray[1][multiIndex[1]].id;
                }
                $.p(app.wxApp.url + 'agentnew/cacheRole', {
                    role: JSON.stringify(role),
                    tel: wx.getStorageSync('tel'),
                }, app.onStrart, (res) => {
                    app.agentStatus();
                }, (res) => {
                    util.showToast(res.data)
                })
            }
        }
    }
})